package com.cauli.manage.notice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cauli.manage.notice.mapper.NoticeMapper;
import com.cauli.manage.notice.model.dto.NoticeAddDTO;
import com.cauli.manage.notice.model.vo.NoticeVO;
import com.cauli.manage.notice.model.dto.NoticeQueryDTO;
import com.cauli.manage.notice.model.dto.NoticeUpdateDTO;
import com.cauli.manage.notice.model.dto.NoticeUpdateStatusDTO;
import com.cauli.manage.notice.model.entity.Notice;
import com.cauli.manage.notice.service.NoticeService;
import com.cauli.utils.exception.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Cauli
 * @date 2022-12-13 15:15:47
 * @description 公告 服务实现类
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Override
    public Page<Notice> getNoticePage(NoticeQueryDTO noticeQueryDTO) {
        return baseMapper.selectPage(noticeQueryDTO.toPage(), noticeQueryDTO.toLambdaQueryWrapper());
    }

    @Override
    public NoticeVO getNoticeById(Long noticeId) {
        Notice notice = baseMapper.selectById(noticeId);
        NoticeVO noticeVO = new NoticeVO();
        if (notice != null) {
            BeanUtils.copyProperties(notice, noticeVO);
        }
        return noticeVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNotice(NoticeAddDTO noticeAddDTO) {
        Notice notice = new Notice();
        BeanUtil.copyProperties(noticeAddDTO, notice);
        if (notice.getTitle() == null) {
            throw new MyException("公告标题为空");
        }
        baseMapper.insert(notice);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNotice(NoticeUpdateDTO noticeUpdateDTO) {
        // 根据公告ID获取公告信息
        Notice notice = baseMapper.selectById(noticeUpdateDTO.getNoticeId());
        BeanUtil.copyProperties(noticeUpdateDTO, notice);
        if (notice == null) {
            throw new MyException("公告不存在");
        }
        baseMapper.updateById(notice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeNoticeStatus(NoticeUpdateStatusDTO noticeUpdateStatusDTO) {
        // 根据公告ID获取公告信息
        Notice notice = baseMapper.selectById(noticeUpdateStatusDTO.getNoticeId());
        if (notice == null) {
            throw new MyException("公告不存在");
        }
        BeanUtil.copyProperties(noticeUpdateStatusDTO, notice);
        baseMapper.updateById(notice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNotice(List<Long> noticeIds) {
        for (Long noticeId : noticeIds) {
            // 根据公告ID获取公告信息
            Notice notice = baseMapper.selectById(noticeId);
            baseMapper.deleteById(notice);
        }
    }
}
